import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ahj_app/app/my/invoicing/setting/viewModel/IncoicSettingListViewModel.dart';
import 'package:flutter_ahj_app/app/my/invoicing/chooseHeader/bean/FP_PresetInvoice_Bean.dart';
import 'package:flutter_ahj_app/base/basewidget/view/BaseWidget.dart';
import 'package:flutter_ahj_app/base/utils/JUIKit/JText.dart';
import 'package:flutter_ahj_app/base/utils/mTopBar.dart';
import 'package:flutter_ahj_app/base/utils/screen_util.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';

class IncoicSettingListView extends BaseStatefulWidget {
  @override
  State<StatefulWidget> initState() {
    // TODO: implement initState
    return _IncoicSettingListViewSate();
  }
}

class _IncoicSettingListViewSate
    extends BaseState<IncoicSettingListViewModel, IncoicSettingListView> {
  List<Data> _dataList = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Future.delayed(Duration.zero, () {
      _getData();
    });
  }

  @override
  Widget initWidget() {
    // TODO: implement initWidget
    return Scaffold(
      backgroundColor: SQColor.lightGray,
      appBar: MTopBar(
          title: '开票设置',
          rightButtonText: '添加',
          onRightButtonClick: () => _pushEvent()).create(),
      body: _dataList.isNotEmpty ? _bodyView() : _noDataView(),
    );
  }

  _bodyView() {
    return _settingListView();
  }

  _settingListView() {
    return Container(
      margin: EdgeInsets.only(top: 10),
      child: ListView.builder(
          itemCount: _dataList.length,
          itemBuilder: (context, index) {
            return _itemView(index);
          }),
    );
  }

  _itemView(int index) {
    Data data = _dataList[index];
    return Column(
      children: <Widget>[
        Card(
          elevation: 0,
          margin: EdgeInsets.only(left: 15, right: 15),
          child: Container(
            decoration: BoxDecoration(
              borderRadius: new BorderRadius.all(Radius.circular(3.0)),
              color: SQColor.white,
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Container(
                        padding: EdgeInsets.only(left: 15, top: 10),
                        child: JText(
                          data?.invoiceName ?? '',
                          fontSize: 16,
                          maxLines: 1,
                          color: SQColor.black_333333,
                          overflow: TextOverflow.ellipsis,
                        ),
                      ),
                      Container(
                        padding: EdgeInsets.only(left: 15, top: 5, bottom: 10),
                        child: JText(
                          data?.taxplayerNumber ?? '',
                          fontSize: 12,
                          color: SQColor.black_666666,
                        ),
                      ),
                    ],
                  ),
                ),
                Row(
                  children: <Widget>[
                    Container(
                      padding: EdgeInsets.only(right: 5),
                      child: JText(
                        '丨',
                        fontSize: 12,
                        color: SQColor.black_999999,
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.only(right: 15),
                      child: InkWell(
                        onTap: () => _pushEvent(index: index),
                        child: JText(
                          '编辑',
                          fontSize: 12,
                          color: SQColor.black_999999,
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ),
        Container(
          color: SQColor.lightGray,
          height: 10,
        ),
      ],
    );
  }

  _noDataView() {
    return Container(
      width: ScreenUtil.getScreenW(context),
      child: Column(
        children: <Widget>[
          Container(
            padding: EdgeInsets.only(top: 100),
            child: Image.asset(
              'img/no_fp.png',
              height: 150,
              width: 167,
            ),
          ),
          Container(
            padding: EdgeInsets.only(top: 20),
            child: JText('暂无预设开票信息', color: SQColor.black_666666, fontSize: 14),
          ),
          new GestureDetector(
            onTap: (){
              _pushEvent();
            },
            child: new Container(
              padding: EdgeInsets.fromLTRB(
                  3, 3, 3, 3),
              margin: EdgeInsets.all(6),
              decoration: BoxDecoration(
                  borderRadius:
                  new BorderRadius.all(
                      Radius.circular(3.0)),
                  color:
                  SQColor.order_btn_text),
              alignment: Alignment.center,
              height: 36,
              child: new Text(
                "添加",
                style: TextStyle(
                    color: SQColor.white),
              ),
            ),
          )
        ],
      ),
    );
  }

  // /// 编辑
  // _editEvent(int index) {
  //   Data data = _dataList[index];
  // }
  /// 跳转
  _pushEvent({int index}) {
    int id = index != null ? _dataList[index].id : null;
    Navigator.pushNamed(context, "/FPHeaderView", arguments: id).then((value) {
      if (value == '请求成功') {

        _getData();
      }
    });
  }

  ///获取数据
  _getData() async {
    viewModel.getData();
    viewModel.dataStream().listen((event) {
      if (event.code == 0 ) {
        if(event.data != null && event.data.length > 0){
          setState(() {
            _dataList = event.data;
          });
        }else{
          setState(() {
            _dataList = [];
          });
        }
      }
    });
  }
}
